#include "COptionsState.h"
#include "VIN_SoundManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "CMainMenuState.h"
#include "CGame.h"
#include "VIN_BitMapFont.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "tinyxml/tinyxml.h"


COptionsMenu::COptionsMenu()
{
	m_nSelection = 0;
	m_nCursor = -1;
	m_nBGM = 1.0f;
	m_nSFX = 1.0f;
	bucket = 0;
	CursorID = CMainMenuState::GetInstance()->GetCursorID();
	winToggle = CSGD_Direct3D::GetInstance()->GetPresentParams()->Windowed;
	m_nBGI = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/Rio.png");
}

COptionsMenu::~COptionsMenu()
{

}

COptionsMenu* COptionsMenu::GetInstance(void)
{
	static COptionsMenu instance;
	return &instance;
}

void COptionsMenu::Enter()
{
	VIN_SoundManager::GetInstance()->PlayBGM(BGM_OTHERMENUS);
	LoadDefaults();
}

void COptionsMenu::Exit()
{
	m_nSelection = 0;
	Save( "resource/XML/Options.xml" );
	//VIN_SoundManager::GetInstance()->StopSound(BGM_OTHERMENUS);

}

void COptionsMenu::Render()
{
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();

	pTM->Draw(m_nBGI,0,0);
	
	RECT MenuRect;
	SetRect( &MenuRect, 150, 100, 600, 400 );

	RECT MenuRectII;
	SetRect( &MenuRectII, 135, 85, 615, 415 );

	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
	CSGD_Direct3D::GetInstance()->DrawRect( MenuRectII, 255,255,255);
	CSGD_Direct3D::GetInstance()->DrawRect( MenuRect, 0,0,0);

	char bufferB[100];
	sprintf_s(bufferB, sizeof(bufferB), "<%.0f>", m_nBGM * 100);
	char bufferS[100];
	sprintf_s(bufferS, sizeof(bufferS), "<%.0f>", m_nSFX * 100);

	pBF->Render(bufferS,500,150, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 0, 0, 200), GAMEFONT);
	pBF->Render(bufferB,500,200, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 0, 0, 200), GAMEFONT);

	pBF->Render( "SFX VOLUME", 200, 150, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 255, 0, 0), MENUFONT);
	pBF->Render( "MUSIC VOLUME", 200, 200, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 255, 0, 0), MENUFONT);
	if(winToggle)	
		pBF->Render( "FULLSCREEN", 200, 250, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 255, 0, 0), MENUFONT);
	else
		pBF->Render( "WINDOWED", 200, 250, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 255, 0, 0), MENUFONT);

	pBF->Render( "EXIT", 200, 300, 0.75f, 0.75f, D3DCOLOR_ARGB(255, 255, 0, 0), MENUFONT);

	switch(m_nSelection)
	{
	case 0:
		{	
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 150, 150);
		}
		break;
	case 1:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 150, 200);
		}
		break;
	case 2:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 150, 250);
		}
		break;
	case 3:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 150, 300);
		}
		break;
	}
}

void COptionsMenu::Update(float fElapsedTime)
{
	VIN_SoundManager::GetInstance()->SetBGMVolume(m_nBGM);
	VIN_SoundManager::GetInstance()->SetSFXVolume(m_nSFX);
	bucket+= fElapsedTime;
}

bool COptionsMenu::Input()
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if( pDI->KeyPressed( DIK_UP ))
	{
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

		m_nSelection--;
		if( m_nSelection == -1 )
			m_nSelection = 3;
	}

	if( pDI->KeyPressed( DIK_DOWN ))
	{
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

		m_nSelection++;
		if( m_nSelection == 4 )
			m_nSelection = 0;
	}

	if( pDI->KeyPressed( DIK_RETURN ))
	{
		if(m_nSelection == 3)
			CGame::GetInstance()->ChangeState( CMainMenuState::GetInstance() );
		if(m_nSelection == 2)
		{
			winToggle = !winToggle;
			CSGD_Direct3D::GetInstance()->ChangeDisplayParam(CSGD_Direct3D::GetInstance()->GetPresentParams()->BackBufferWidth, 
				CSGD_Direct3D::GetInstance()->GetPresentParams()->BackBufferHeight,winToggle);
		}
	}

	if( pDI->KeyPressed( DIK_ESCAPE ))
			CGame::GetInstance()->ChangeState( CMainMenuState::GetInstance() );

	if( pDI->KeyDown( DIK_LEFT ))
	{
		if( m_nSelection == 0 )
		{
			if(bucket >=.1f)
			{
				m_nSFX-= 0.05f;
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();
				if( m_nSFX < 0 )
				{
					m_nSFX=0;
				}
				bucket = 0;
			}
		}
		else if( m_nSelection == 1 )
		{
			if(bucket >=.1f)
			{
				m_nBGM-= 0.05f;
				if( m_nBGM < 0 )
				{
					m_nBGM = 0;
				}
				bucket = 0;
			}
		}
	}
	if( pDI->KeyDown( DIK_RIGHT ))
	{
		if( m_nSelection == 0 )
		{
			if(bucket >=.1f)
			{
				m_nSFX+=0.05f;
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();
				if( m_nSFX > 1.0f )
				{
					m_nSFX = 1.0f;
				}
				bucket = 0;
			}
		}
		else if( m_nSelection == 1 )
		{
			if(bucket >=.1f)
			{
				m_nBGM+=0.05f;	
				if( m_nBGM > 1.0f )
				{
					m_nBGM = 1.0f;
				}
				bucket = 0;
			}
		}
	}
	return true;
}

void COptionsMenu::LoadDefaults()
{
		TiXmlDocument doc;

	if( doc.LoadFile("resource/XML/Options.xml") == false)
		return;

	TiXmlElement* Root = doc.RootElement();
	if( Root == NULL )
		return;

		TiXmlElement* pOptions = Root->FirstChildElement("Option");
		double Music;
		double SFX;
		bool WindowStatus;

		pOptions->Attribute("Music", &Music);
		pOptions->Attribute("SFX", &SFX );

		m_nBGM = (float)Music;
		m_nSFX = (float)SFX;
		//winToggle = WindowStatus;
}


bool COptionsMenu::Save(const char* filename)
{
	TiXmlDocument doc;

	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0", "utf-8", "");

	// 1. Cut a hole in the box
	// 2. Put your dec in the box
	doc.LinkEndChild(pDec);

	TiXmlElement* pRoot = new TiXmlElement("Options");

	doc.LinkEndChild(pRoot);

		TiXmlElement* pOptions = new TiXmlElement("Option");

		pOptions->SetAttribute("Music", (int)m_nBGM);

		pOptions->SetAttribute("SFX", (int)m_nSFX);

		// put Option in Options
		pRoot->LinkEndChild(pOptions);

	if(doc.SaveFile(filename) == false)
		return false;

	return true;
}